﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Core.Extensions;
using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations;
using TiaoTaoHelper.Wtm.Models;


namespace HengTong.ViewModel._Admin.SysJobPositionVms
{
    public partial class SysJobPositionListVm : BasePagedListVM<SysJobPositionView, SysJobPositionSearcher>
    {
        protected override IEnumerable<IGridColumn<SysJobPositionView>> InitGridHeader()
        {
            return new List<GridColumn<SysJobPositionView>>
            {
                this.MakeGridHeader(x => x.Code),
                this.MakeGridHeader(x => x.Name),
                this.MakeGridHeader(x => x.Description),
                this.MakeGridHeader(x => x.GroupName),
                this.MakeGridHeader(x => x.RolesName),
                this.MakeGridHeader(x => x.Step),
                this.MakeGridHeader(x => x.IsManager),
                this.MakeGridHeader(x => x.Order),
                this.MakeGridHeader(x => x.Salary),
                this.MakeGridHeaderAction(width: 200)
            };
        }

        public override IOrderedQueryable<SysJobPositionView> GetSearchQuery()
        {
            var query = DC.Set<SysJobPosition>()
                .CheckContain(Searcher.Code, x => x.Code)
                .CheckContain(Searcher.Name, x => x.Name)
                .CheckEqual(Searcher.GroupId, x => x.GroupId)
                .CheckWhere(Searcher.SelectedRolesIds, x => DC.Set<SysJobPositionRole>()
                    .Where(y => Searcher.SelectedRolesIds.Contains(y.RoleId.Value))
                    .Select(z => z.JobPositionId).Contains(x.ID))
                .Select(x => new SysJobPositionView
                {
                    ID = x.ID,
                    Code = x.Code,
                    Name = x.Name,
                    Description = x.Description,
                    GroupName = x.Group.GroupName,
                    RolesName = x.Roles.Select(y => y.Role.RoleName).ToSepratedString(null, ","),
                    Step = x.Step,
                    IsManager = x.IsManager,
                    Order = x.Order,
                    Salary = x.Salary,
                })
                .OrderBy(x => x.Order ?? 99.99m)
                .ThenBy(x => x.ID);
            return query;
        }
    }

    public class SysJobPositionView : SysJobPosition
    {
        [Display(Name = "全称")] public String GroupName { get; set; }
        [Display(Name = "_Admin.RoleName")] public String RolesName { get; set; }
    
        [Display(Name = "排序")]
        public decimal? Order { get; set; }
    }
}